package com.tushen.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * create by likun on 2020/7/16 0016
 */
public class Utils {

    /**
     * 读取双精度二维数组
     * @param path
     * @return
     */
    public static double[][] readTxtToArray(String path) {
        List<List<Double>> conList = new ArrayList<List<Double>>();
        double[][] douline = null;
        try {
            File file = new File(path);
            String encoding = "UTF-8";
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = "";
                int len = 0;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    if(lineTxt.length()==0)continue;
                    String[] myArray=lineTxt.trim().replaceAll("\\s+",",").split(",");
                    List<Double> lineList = new ArrayList<Double>();

                    for (String str : myArray) {

                        lineList.add(Double.parseDouble(str));
                    }
                    conList.add(lineList);
                    len++;

                }
                read.close();
                douline = new double[conList.size()][conList.get(0).size()];
                for (int i = 0; i <conList.size(); i++) {
                    for (int j = 0; j < conList.get(i).size(); j++) {
                        if(!conList.get(i).isEmpty()&&conList.get(i).size()!=0){
                            douline[i][j]=conList.get(i).get(j);
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return douline;
    }

    /**
     * 读取整数二维数组
     * @param path
     * @return
     */
    public static int[][] readTxtToArrayIntenger(String path) {
        List<List<Integer>> conList = new ArrayList<List<Integer>>();
        int[][] douline = null;
        try {
            File file = new File(path);
            String encoding = "UTF-8";
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = "";
                int len = 0;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    if(lineTxt.length()==0)continue;
                    String[] myArray=lineTxt.trim().split(",");
                    List<Integer> lineList = new ArrayList<Integer>();

                    for (String str : myArray) {

                        lineList.add(Integer.parseInt(str));
                    }
                    conList.add(lineList);
                    len++;

                }
                read.close();
                douline = new int[conList.size()][conList.get(0).size()];
                for (int i = 0; i <conList.size(); i++) {
                    for (int j = 0; j < conList.get(i).size(); j++) {
                        if(!conList.get(i).isEmpty()&&conList.get(i).size()!=0){
                            douline[i][j]=conList.get(i).get(j);
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return douline;
    }


    public static double[][] readTxtToArray2(String path) {
        List<List<Double>> conList = new ArrayList<List<Double>>();
        double[][] douline = null;
        try {
            File file = new File(path);
            String encoding = "UTF-8";
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = "";
                int len = 0;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    if(lineTxt.length()==0)continue;
                    String[] myArray=lineTxt.trim().replaceAll("\\s+",",").split(",");
                    List<Double> lineList = new ArrayList<Double>();

                    for (String str : myArray) {

                        lineList.add(Double.parseDouble(str));
                    }

                    len++;
                    if(len>4000){
                        conList.add(lineList);
                        break;
                    }

                }
                read.close();
                douline = new double[conList.size()][conList.get(0).size()];
                for (int i = 0; i <conList.size(); i++) {
                    for (int j = 0; j < conList.get(i).size(); j++) {
                        if(!conList.get(i).isEmpty()&&conList.get(i).size()!=0){
                            douline[i][j]=conList.get(i).get(j);
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return douline;
    }

    public static double getDirection(double u,double v){
        double dirction=0;
        if(u==0&&v>0){
            dirction=0;
        }else if(u>0&&v>0){
            dirction=Math.atan(u/v)*57.2926;
        }else if(u>0&&v==0){
            dirction=90;
        }else if(u>0&&v<0){
            dirction=Math.abs(Math.atan(u/v))*57.2926+90;
        }else if(u==0&&v<0){
            dirction=180;
        }else if(u<0&&v<0){
            dirction=Math.abs(Math.atan(u/v))*57.2926+180;
        }else if(u<0&&v==0){
            dirction=270;
        }else if(u<0&&v>0){
            dirction=Math.abs(Math.atan(u/v))*57.2926+270;
        }

        return dirction;
    }

    public static void copy(String srcPathStr, String desPathStr)
    {
        //获取源文件的名称
//        String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
//        System.out.println("源文件:"+newFileName);
//        desPathStr = desPathStr + File.separator + newFileName; //源文件地址
//        System.out.println("目标文件地址:"+desPathStr);
        try
        {
            FileInputStream fis = new FileInputStream(srcPathStr);//创建输入流对象
            FileOutputStream fos = new FileOutputStream(desPathStr); //创建输出流对象
            byte datas[] = new byte[1024*8];//创建搬运工具
            int len = 0;//创建长度
            while((len = fis.read(datas))!=-1)//循环读取数据
            {
                fos.write(datas,0,len);
            }
            fis.close();//释放资源
            fis.close();//释放资源
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
